package com.gxzy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxzy.config.VarPool;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 请求体工具类
 */

public class ReqUtil {
    private static final Logger logger = LogManager.getLogger(ReqUtil.class);

    /**
     * 获取请求体
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    /**
     * 获取响应体
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }

    /**
     * 页面级请求验证
     *
     * @param request
     * @return
     */
    public static boolean isPageReq(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        return accept != null && accept.contains("text/html");
    }

    /**
     * 获取请求体内容
     *
     * @return
     * @throws IOException
     */
    public static Map getParamsFromRequestBody(HttpServletRequest request) {
        BufferedReader reader = null;
        try {
            reader = request.getReader();
        } catch (IOException e) {
            return new HashMap<>();
        }

        StringBuilder builder = new StringBuilder();
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            String bodyString = builder.toString();
            if (bodyString != null && bodyString.length() > 2) {
                JSONObject jsonObject = JSON.parseObject(bodyString);
                Map<String, Object> map = jsonObject;
                return map;
            } else {
                return new HashMap();
            }
        } catch (Exception e) {
//            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
//                e.printStackTrace();
            }
        }
        return new HashMap<>();
    }

    /**
     * 判断请求路径是否存在于excludeStr规则中
     *
     * @param request    请求体
     * @param excludeStr 路径规则
     * @return
     */
    public static boolean isFilterExcludeRequest(HttpServletRequest request, Iterable<String> excludeStr) {
        String url = request.getServletPath();
        for (String ex : excludeStr) {
            if (isFilterExcludeRequest(url, ex)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 资源放行
     *
     * @param request 请求体
     * @param excludeStr 放行数据
     * @return
     */
    public static boolean isFilterExcludeRequest(HttpServletRequest request, String[] excludeStr) {
        String url = request.getServletPath();
        for (String ex : excludeStr) {
            if (isFilterExcludeRequest(url, ex)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 资源放行
     *
     * @param request 请求体
     * @param excludeStr 放行数据
     * @return
     */
    public static boolean isFilterExcludeRequest(HttpServletRequest request, HashMap<String, Boolean> excludeStr) {
        String url = request.getServletPath();
        Set<String> strings = excludeStr.keySet();
        for (String ex : strings) {
            if (excludeStr.get(ex) && isFilterExcludeRequest(url, ex)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 资源放行
     *
     * @param url
     * @param ex
     * @return
     */
    public static boolean isFilterExcludeRequest(String url, String ex) {
        /* 空数据校验 */
        if (!CheckUtil.dao.isStrNotNull(ex)) {
            return false;
        }

        /* 相等则直接放行 */
        if (url.equals(ex)) {
            return true;
        }

        /* 前缀部分相等则放行 */
        int index = ex.indexOf("/**");
        if (index != -1 && url.length() >= index) {
            if (url.substring(0, index).equals(ex.substring(0, index))) {
                return true;
            }
        }

        /* 后缀部分相等则放行 */
        index = ex.indexOf("**/");
        if (index != -1 && url.length() >= (index + 3)) {
            index += 3;
            if (url.substring(index).equals(ex.substring(index))) {
                return true;
            }
        }

        return false;
    }

}